package com.example.hp.yuejian5.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.ActivityorderBean;
import com.example.hp.yuejian5.model.ActivityorderModel;

public class ActivityorderActivity extends AppCompatActivity{
    private Button submit;
    private Button r_button;
    private String activity_id,user_id;
    private SharedPreferences sp;
    private EditText realname,tel,anum,snum,card;
    private RetrofitListener<ActivityorderBean> listener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activityorder);
        initView();
        initEvents();
        Intent intent = getIntent();
        activity_id = (String) intent.getStringExtra("a_id");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getString("userid", null);

        listener=new RetrofitListener<ActivityorderBean>(){
            @Override
            public void onSuccess(ActivityorderBean activityorderBean, int flag) {
                if(activityorderBean.getSuccess().equals("1"))
                {
                    Toast.makeText(ActivityorderActivity.this,"报名成功",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(ActivityorderActivity.this,FragmentActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                    {
                        Toast.makeText(ActivityorderActivity.this,"已报名",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {
                Toast.makeText(ActivityorderActivity.this,"报名失败",Toast.LENGTH_SHORT).show();
            }
        };
    }
    private void initView(){
        submit=findViewById(R.id.submit);
        r_button=findViewById(R.id.r_button);
        realname=findViewById(R.id.realname);
        tel =findViewById(R.id.tel);
        anum=findViewById(R.id.anum);
        snum=findViewById(R.id.snum);
        card=findViewById(R.id.card);
    }
private void initEvents(){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        r_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                return_b();
            }
        });
}
private void register(){
    ActivityorderModel activityorderModel=new ActivityorderModel();
    activityorderModel.Activityorder(user_id,activity_id,realname.getText().toString().trim(),
            tel.getText().toString().trim(),
            anum.getText().toString().trim(),
            snum.getText().toString().trim(),
            card.getText().toString().trim(),
            "支付宝",
            "1",
            listener);
}
private  void return_b(){
    Intent intent=new Intent(ActivityorderActivity.this,Activity_DetailActivity.class);
    startActivity(intent);
    finish();
}
}
